package top.hidoctor.auscultation.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.tang.util.BigDecimalUtil;
import com.tang.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

import top.hidoctor.auscultation.base.Constant;


public class LocEcgView extends View {
    private static final String TAG = "view";
    private int width;
    private int heigh;

    //间距
    private float gridspace_width;

    //背景的画笔
    private Paint mPaint_bg;

    //折线的画笔
    private Paint mPaint_brokenline;
    //路径
    private Path mpath = new Path();
    private List<Float> mdata = new ArrayList<>();
    private Context context;

    public LocEcgView(Context context) {
        super(context);
        inite(context);
    }

    public LocEcgView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inite(context);
    }

    private void inite(Context context) {
        this.context = context;
        //灰色背景的画笔
        mPaint_bg = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint_bg.setColor(Color.parseColor("#444444"));


        //折线的画笔
        mPaint_brokenline = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint_brokenline.setColor(Color.parseColor("#0F88F2"));
        mPaint_brokenline.setTextSize(18);
        mPaint_brokenline.setTextAlign(Paint.Align.CENTER);

        invalidate();
    }

    public void setDate(List<Float> list, float gap_x) {

        gridspace_width = gap_x;
        // -0.55 到 0.55 之间数据  转变 0-110
        for (int i = 0; i < list.size(); i++) {
            mdata.add((float) (list.get(i) + 0.55) * 100);
        }

        requestLayout();//相当于调用onMeasure方法
        invalidate();//相当于调用onDraw方法
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.e(TAG, "onDraw: ");
        //绘制折线图的白色背景
//        canvas.drawColor(Color.WHITE);
        //绘制灰色矩形区域
//        canvas.drawRect(0, 0, width, heigh, mPaint_bg);

        for (int i = 0; i < mdata.size(); i++) {
            if (i == 0) {
                mpath.moveTo(0,
                        heigh - heigh * mdata.get(i) / 110);
            }
            if (i != mdata.size() - 1) {
                //根据圆点位置绘制折线
                canvas.drawLine(
                        gridspace_width * i,
                        (float) (heigh - heigh * mdata.get(i) / 110),
                        gridspace_width * (i + 1),
                        (float) (heigh - heigh * mdata.get(i + 1) / 110),
                        mPaint_brokenline);
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //设置底部白色空白的宽度
        if (mdata != null) {
            width = (int) BigDecimalUtil.mul(gridspace_width, mdata.size());
        }
        heigh = DensityUtil.dip2px(context, 100);
        setMeasuredDimension(width, heigh);
    }

}
